package cn.xiaopengstack.domain.strategy.service.rule.tree.impl;

import cn.xiaopengstack.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import cn.xiaopengstack.domain.strategy.model.valobj.RuleTreeNodeVO;
import cn.xiaopengstack.domain.strategy.service.rule.tree.ILogicTreeNode;
import cn.xiaopengstack.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.security.SecureRandom;
import java.util.Random;

/**
 * @author jiangyangang
 */
@Component("rule_luck_award")
@Slf4j
public class RuleLuckAwardLogicTreeNode implements ILogicTreeNode {
    @Override
    public DefaultTreeFactory.TreeActionEntity logic(DefaultTreeFactory.TreeActionMatterEntity treeMatter) {
        RuleTreeNodeVO ruleTreeNodeVO = treeMatter.getRuleTreeMap().get("rule_luck_award");
        String[] ruleValueSplit = ruleTreeNodeVO.getRuleValue().split(Constants.COLON);
        int awardId = Integer.parseInt(ruleValueSplit[0]);
        String[] scoreSplit = ruleValueSplit[1].split(Constants.SPLIT);
        int minScore = Integer.parseInt(scoreSplit[0]);
        int maxScore = Integer.parseInt(scoreSplit[1]);

        // 随机
        int randomScore = new SecureRandom().nextInt(maxScore - minScore + 1) + minScore;

        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .strategyAwardVo(DefaultTreeFactory.StrategyAwardVO.builder()
                        .awardId(awardId)
                        .awardRuleValue(randomScore + "")
                        .build())
                .build();
    }
}
